package com.jeeplus.mob.common;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StringHelper{
  private static final Logger logger = LoggerFactory.getLogger(StringHelper.class);
  public static final String EMPTY = "";
  
  //将参数转换成BigDecimal类型
  public static BigDecimal getBigDecimal(Object value) {
	  
	  BigDecimal ret = null;
	  if (value != null) {
		  if (value instanceof BigDecimal) {
			  ret = (BigDecimal) value;
		  } else if (value instanceof String) {
			  ret = new BigDecimal((String) value);
		  } else if (value instanceof BigInteger) {
			  ret = new BigDecimal((BigInteger) value);
		  } else if (value instanceof Number) {
			  ret = new BigDecimal(((Number) value).doubleValue());
		  } else {
			  throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
			  + " into a BigDecimal.");
		  }
	  }
	  return ret;
	  }
  
  public static Boolean IsEmptyOrNull(Object obj){
	  if (obj == null) {
		  return Boolean.valueOf(true);
	  }
	  if ("".equals(obj)) {
		  return Boolean.valueOf(true);
	  }
	  if ((obj instanceof String))
	  {
		  if (((String)obj).length() == 0) {
			  return Boolean.valueOf(true);
		  }
	  }
	  else if ((obj instanceof Collection))
	  {
		  if (((Collection)obj).size() == 0) {
			  return Boolean.valueOf(true);
		  }
	  }
	  else if (((obj instanceof Map)) && 
			  (((Map)obj).size() == 0)) {
		  return Boolean.valueOf(true);
	  }
	  return Boolean.valueOf(false);
  }
  
  public static String TranString(Object obj){
	  if (obj == null) {
		  return "";
	  }
	  return obj.toString();
  }

  public static String TranString(Object obj, String defaultval)
  {
	  if (obj == null) {
		  return defaultval;
	  }
	  return obj.toString();
  }

  public static String TranSeachString(Object obj){
	  if (obj == null) {
		  return "";
	  }
	  String result = obj.toString();

	  return result.replace("\\", "\\\\").replace("_", "\\_").replace("%", "\\%");
  }

  public static String ByteToString(byte[] srcobj)
  {
	  return ByteToString(srcobj, "UTF-16LE");
  }

  public static String ByteToString(byte[] srcObj, String charEncode){
	  String destObj = null;
	  try
	  {
		  destObj = new String(srcObj, charEncode);
	  }
	  catch (Exception e)
	  {
		  logger.error("转换错误：" + e.getMessage());
	  }
	  return destObj.replaceAll("", " ");
  }

  public static String GetGUID(){
	  return UUID.randomUUID().toString().replace("-", "");
  }

  /**
   * 使用递归算法，获取第一个随机数不为0
   * @return int
   */
  public static int getRandom(){
	  int number = new Random().nextInt(10);
	  if(0 == number){
		  return getRandom();
	  }
	  return number;
  }
  
  //数组排序
  public static List<Map<String, Object>> listSort(List<Map<String, Object>> list,final String orderByName,final String order){
	  Collections.sort(list, new Comparator<Map<String, Object>>() {
          public int compare(Map<String, Object> o1, Map<String, Object> o2) {
        	  Double SumCus1 = Double.valueOf(o1.get(orderByName).toString()) ;//name1是从你list里面拿出来的一个 
        	  Double SumCus2 = Double.valueOf(o2.get(orderByName).toString()) ; //name1是从你list里面拿出来的第二个name
              if (order.equals("desc")) {
            	  return SumCus2.compareTo(SumCus1);
              } else {
            	  return SumCus1.compareTo(SumCus2);
              }
          }
      });
	return list;
  }

}

